﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLNS.Class;
using QLNS.NghiepVu;

namespace QLNS.NghiepVu
{
    public partial class frmSuaQuyetDinhKhenThuongKyLuat : Form
    {
        public frmSuaQuyetDinhKhenThuongKyLuat()
        {
            InitializeComponent();
        }
        DB cn = new DB();

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //sao không có form quan lý qd ở đây?
        

        private void load_dtgChiTiet()
        {
            if (cn.KetNoi())
            {
                DataTable tbl = cn.getTable("SELECT ChiTietKhenThuongKyLuat.MaNhanVien, NhanVien.HoTen, ChiTietKhenThuongKyLuat.MucThuongPhat FROM  ChiTietKhenThuongKyLuat INNER JOIN NhanVien ON ChiTietKhenThuongKyLuat.MaNhanVien = NhanVien.MaNhanVien where ChiTietKhenThuongKyLuat.SoQD='" + frmQuanLyQDKhenThuongKyLuat._SoQD + "'");
                dtgChiTiet.DataSource = tbl;

            }
        }
        private void load_combo()
        {
            cn.KetNoi();
            cmbQD.DataSource = cn.getTable("select *from KhenThuongKyLuat");
            cmbQD.DisplayMember = "TenLoai"; 
            cmbQD.ValueMember = "MaLoai";

            //cmbNhanVien.DataSource = cn.getTable("select *from NhanVien");
            //cmbNhanVien.DisplayMember = "HoTen";
            //cmbNhanVien.ValueMember = "MaNhanVien";
        }
        private void frmSuaQuyetDinhKhenThuongKyLuat_Load(object sender, EventArgs e)
        {
            cn.KetNoi();
            load_dtgChiTiet();
            load_combo();

            txtSoQD.Text = frmQuanLyQDKhenThuongKyLuat._SoQD;
            dtpNgayQD.Value = DateTime.Parse(frmQuanLyQDKhenThuongKyLuat._NgayQD);
            txtNoiDung.Text = frmQuanLyQDKhenThuongKyLuat._NoiDung;
            txtHinhThuc.Text = frmQuanLyQDKhenThuongKyLuat._HinhThuc;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult h = MessageBox.Show("Bạn có muốn thoát không?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question );
            if(h==DialogResult.Yes )
            {
                this.Close();
            }
        }

        private void dtgChiTiet_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgChiTiet.RowCount > 0)
            {
                txtNhanVien.Text = dtgChiTiet.CurrentRow.Cells["MaNhanVien"].Value.ToString();
                txtMucThuongPhat.Text = dtgChiTiet.CurrentRow.Cells["MucThuongPhat"].Value.ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dtgChiTiet.Rows.Count - 1; i++)
            {
                dtgSuaCT.Rows.Add(dtgChiTiet.Rows[i].Cells["MaNhanVien"].Value.ToString(),dtgChiTiet.Rows[i].Cells["HoTen"].Value.ToString(),txtMucThuongPhat.Text);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dtgSuaCT.Rows.Clear();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            cn.Update_Table("QDKhenThuongKyLuat","NgayQD='"+dtpNgayQD.Value.ToString("yyyy-MM-dd")+"',MaLoai='"+cmbQD.SelectedValue.ToString()+"',NoiDung=N'"+txtNoiDung.Text+"',HinhThuc=N'"+txtHinhThuc.Text+"'","SoQD='"+txtSoQD.Text+"'");
           
                for (int i = 0; i < dtgSuaCT.Rows.Count - 1; i++)
                {
                    cn.Update_Table("ChiTietKhenThuongKyLuat","MaNhanVien='"+dtgSuaCT.Rows[i].Cells["MaNV"].Value.ToString()+"',MucThuongPhat='"+dtgSuaCT.Rows[i].Cells["MucTP"].Value.ToString()+"'","SoQD='"+txtSoQD.Text+"'");
                }
                MessageBox.Show("Thành công");
                
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            dtgChiTiet.Refresh();
            dtgSuaCT.Rows.Clear();
            
        }
    }
}
